;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:          artist.lisp
;;;; Purpose:       Audioscrobbler/lastfm Artist API.
;;;; Programmer:    Nicolas Lamirault <nicolas.lamirault@gmail.com>
;;;;
;;;; This file, part of audioscrobbler, is Copyright (c) 2007 by Nicolas Lamirault
;;;;
;;;; cl-audioscrobbler users are granted the rights to distribute and use this software
;;;; as governed by the terms of the MIT License :
;;;; http://www.opensource.org/licenses/mit-license.php
;;;;
;;;; *************************************************************************


(in-package :cl-audioscrobbler)


(defun retrieve-artist-related (artist)
 "People who listen to ARTIST-NAME also listen to ..."
 (get-xml-answer (format nil +artist-related+ artist)))


(defun retrieve-artist-top-albums (artist)
 "The most popular albums by a given ARTIST."
 (get-xml-answer (format nil +artist-top-albums+ artist)))


(defun retrieve-artist-top-tracks (artist)
 "The most popular tracks by a given ARTIST."
 (get-xml-answer (format nil +artist-top-tracks+ artist)))


(defun retrieve-artist-top-fans (artist)
 "Profiles who've listened to a given ARTIST the most,
i.e. the biggest fans."
 (get-xml-answer (format nil +artist-top-fans+ artist)))


(defun retrieve-artist-top-tags (artist)
  "The most popular tags applied to a given ARTIST."
  (get-xml-answer (format nil +artist-top-tags+ artist)))



;; ----------------
;; API WebServices
;; ----------------


(defun get-artist-related (artist)
 "A list of people who listen to ARTIST also listen to A,B,C..."
 (let ((xmldoc (retrieve-artist-related artist)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
        as name = (cadr (first (cdr sexp)))
        as mbid = (if (listp (second (cdr sexp)))
                      (cadr (second (cdr sexp)))
                      "")
        as url = (cadr (third (cdr sexp)))
        as image-small = (cadr (fourth (cdr sexp)))
        as image = (cadr (fifth (cdr sexp)))
        collect (make-instance 'artist-related
                               :name name
                               :mbid mbid
                               :url url
                               :image-small image-small
                               :image image)))))


(defun get-artist-top-albums (artist)
 "A list of the most popular albums by a given ARTIST."
 (let ((xmldoc (retrieve-artist-top-albums artist)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
          as name = (cadr (first (cdr sexp)))
          as mbid = (if (listp (second (cdr sexp)))
                      (cadr (second (cdr sexp)))
                      "")
          as reach = (cadr (third (cdr sexp)))
          as url = (cadr (fourth (cdr sexp)))
          collect (make-instance 'artist-top-album
                                 :name name
                                 :mbid mbid
                                 :reach reach
                                 :url url)))))


(defun get-artist-top-tracks (artist)
 "A list of the most popular tracks by a given ARTIST."
 (let ((xmldoc (retrieve-artist-top-tracks artist)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
          as name = (cadr (first (cdr sexp)))
          as mbid = (if (listp (second (cdr sexp)))
                      (cadr (second (cdr sexp)))
                      "")
          as reach = (cadr (third (cdr sexp)))
          as url = (cadr (fourth (cdr sexp)))
          collect (make-instance 'artist-top-track
                                 :name name
                                 :mbid mbid
                                 :reach reach
                                 :url url)))))


(defun get-artist-top-fans (artist)
 "A list profiles of the biggest ARTIST's fans."
 (let ((xmldoc (retrieve-artist-top-fans artist)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
        as name = (cadr (first (cdr sexp)))
        as url = (cadr (second sexp))
        as image = (cadr (third sexp))
        as weight = (cadr (fourth sexp))
        collect (make-instance 'fan
                               :name name
                               :url url
                               :image image
                               :weight weight)))))


(defun get-artist-top-tags (artist)
 "A list of the most popular tags applied to a given ARTIST."
 (let ((xmldoc (retrieve-artist-top-tags artist)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
        as name = (cadr (first (cdr sexp)))
        as url = (cadr (second sexp))
        as count = (cadr (third sexp))
        collect (make-instance 'tag
                               :name name
                               :url url
                               :count count)))))




